package com.wangzhen.test.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @author wz
 * @company 公司
 * @create 2020-04-14 17:43
 */
@Configuration
public class MQConfig {
    //死信交换机
    public static final String BUSINESS_EXCHANGE_DEAD_LETTER = "business_exchange_dead_letter";
    //timeout队列
    public static final String QUEUE_ORDER_TIMEOUT = "queue_order_timeout";
    //create队列
    public static final String QUEUE_ORDER_CREATE = "queue_order_create";

    // 声明死信Exchange
    @Bean(BUSINESS_EXCHANGE_DEAD_LETTER)
    public Exchange BUSINESS_EXCHANGE_DEAD_LETTER() {
        Exchange build = ExchangeBuilder.fanoutExchange(BUSINESS_EXCHANGE_DEAD_LETTER).durable(true).build();
        System.out.println(build.toString());
        return build;
    }

    //声明队列
    @Bean(QUEUE_ORDER_TIMEOUT)
    public Queue QUEUE_ORDER_TIMEOUT(){
        return new Queue(QUEUE_ORDER_TIMEOUT);
    }


    //声明队列
    @Bean
    public Queue QUEUE_ORDER_CREATE(){
        //Map<String, Object> args = new HashMap<>(3);
        //        args.put("x-message-ttl", 10 * 1000);
        //              x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        //        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        //       x-dead-letter-routing-key  这里声明当前队列的死信路由key
        //        args.put("x-dead-letter-routing-key", DEAD_LETTER_REALTIMEMSG_ROUTING_KEY);
        //        return QueueBuilder.durable(BUSINESS_DELAY_REALTIMEMSG_NAME).withArguments(args).build();
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 10 * 1000);
        args.put("x-dead-letter-exchange", BUSINESS_EXCHANGE_DEAD_LETTER);
        Queue queue = new Queue(QUEUE_ORDER_CREATE, true, false, false,args);
        return queue;
    }


    @Bean
    public Binding ORDER_TIMEOUT_EXCHANGE_BINDING(@Qualifier(QUEUE_ORDER_TIMEOUT) Queue queue, @Qualifier(BUSINESS_EXCHANGE_DEAD_LETTER)Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
}
